/**
  ******************************************************************************
  * @file    RS485_DRIVER.h
  * @author  Feirui_Joe
  * @version V0.1
  * @date    07/26/2010
  * @brief   USART operation
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 

#ifndef  __RS485_DRIVER_H__
#define  __RS485_DRIVER_H__

#ifdef __cplusplus
 extern "C" {
#endif


/**
  * @brief GLOBALS RAM definition
  */
#ifdef   RS485_DRIVER_GLOBALS
#define  RS485_DRIVER_EXT
#else
#define  RS485_DRIVER_EXT  extern
#endif


RS485_DRIVER_EXT uint8_t RS485RXWorkMode;
RS485_DRIVER_EXT uint8_t RS485RXOverTiCNT;



#define RS485TIM_SetCompare                   TIM_SetCompare3
#define RS485TXDMA_Channel_IRQHandler         DMA1_Channel4_IRQHandler





/* USART Cfg */
#define APP_RS485_CLK1                        RCC_APB2Periph_USART1
#define APP_RS485_GPIO_CLK                    (RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC)

#define APP_RS485_RTXGPIO                     GPIOA
#define APP_RS485_RXPin                       GPIO_Pin_10
#define APP_RS485_TXPin                       GPIO_Pin_9
#define APP_RS485_CONGPIO                     GPIOC
#define APP_RS485_CONPin                      GPIO_Pin_13

#define APP_RS485                             USART1
#define APP_RS485_BaudRate                    9600
#define APP_RS485_WordLength                  USART_WordLength_8b
#define APP_RS485_StopBits                    USART_StopBits_2
#define APP_RS485_Parity                      USART_Parity_No
#define APP_RS485_HardwareFlowControl         USART_HardwareFlowControl_None
#define APP_RS485_Mode                        (USART_Mode_Rx | USART_Mode_Tx)

#define APP_RS485_IRQChannel                  USART1_IRQn
#define APP_RS485_IRQPrePrio                  0x0E
#define APP_RS485_IRQSubPrio                  0x0E
#define APP_RS485_RX_ITx                      USART_IT_RXNE
#define APP_RS485_RX_FLAGx                    USART_FLAG_RXNE
#define APP_RS485_TX_ITx                      USART_IT_TC
#define APP_RS485_TX_FLAGx                    USART_FLAG_TC

#define APP_RS485_TXE_ITx                     USART_IT_TXE
#define APP_RS485_TXE_FLAGx                   USART_FLAG_TXE


/* DMA Cfg */
#define RS485RX_DR_Address                    ((uint32_t)0x40013804)
#define RS485TX_DR_Address                    ((uint32_t)0x40013804)
                                              
#define RS485DMA_RCCPeriph                    RCC_AHBPeriph_DMA1

#define RS485RXDMA_IRQChannel                 USART1_IRQn//DMA1_Channel3_IRQn
#define RS485RXDMA_IRQPrePrio                 0x0E
#define RS485RXDMA_IRQSubPrio                 0x0F
                
                                              
#define RS485TXDMA_IRQChannel                 DMA1_Channel4_IRQn
#define RS485TXDMA_IRQPrePrio                 0x0E
#define RS485TXDMA_IRQSubPrio                 0x0F
                                              
#define RS485TXDMA_Channel                    DMA1_Channel4
#define RS485TXDMA_Channel_IT                 DMA_IT_TC
#define RS485TXDMA_IT_TCMode                  DMA1_IT_TC4
#define RS485TXDMA_IT_GLMode                  DMA1_IT_GL4
#define RS485TXDMA_FLAG_Mode                  DMA1_FLAG_TC4
                                              
#define RS485TXDMA_PeripheralBaseAddr         ((uint32_t)RS485TX_DR_Address)  
#define RS485TXDMA_MemoryBaseAddr             ((uint32_t)RS485TXBufferData)
#define RS485TXDMA_DIR                        DMA_DIR_PeripheralDST                                         
#define RS485TXDMA_PeripheralInc              DMA_PeripheralInc_Disable       
#define RS485TXDMA_MemoryInc                  DMA_MemoryInc_Enable            
#define RS485TXDMA_PeripheralDataSize         DMA_PeripheralDataSize_Byte 
#define RS485TXDMA_MemoryDataSize             DMA_MemoryDataSize_Byte     
#define RS485TXDMA_Mode                       DMA_Mode_Normal                 
#define RS485TXDMA_Priority                   DMA_Priority_High               
#define RS485TXDMA_M2M                        DMA_M2M_Disable                 



RS485_DRIVER_EXT void RS485DateSendCOMM_handling(uint8_t *Point,uint16_t DataNumber);
RS485_DRIVER_EXT void PORInit_RS485Interface(void);
RS485_DRIVER_EXT void RS485USART_IRQHandler(void);
RS485_DRIVER_EXT void RS485TXDMA_Channel_IRQHandler(void);
RS485_DRIVER_EXT void NVIC_RS485Config(FunctionalState TIMCtrSta);

#ifdef __cplusplus
}
#endif

#endif	/*	__RS485_DRIVER_H__	*/
